//
//  HitBubbleViewController.swift
//  funcode
//
//  Created by Wan Jiading on 2016/12/5.
//  Copyright © 2016年 WanJiading. All rights reserved.
//	打泡泡small game

import UIKit

class HitBubbleViewController: UIViewController {

	let width: CGFloat = 30
	let height: CGFloat = 30
	let countPerH: Int = 3 		// 每行数量
	let countPerV: Int = 3		// 每列数量
	var marginH: CGFloat = 0	// X轴 间距
	var marginV: CGFloat = 0 	// Y轴 间距
	
    override func viewDidLoad() {
        super.viewDidLoad()
		self.view.backgroundColor = UIColor.white
		
		for _ in 0..<countPerH * countPerV {
			let btn = createBtn()
			self.view.addSubview(btn)
		}
    }
	
	override func viewDidLayoutSubviews() {
		super.viewDidLayoutSubviews()
		
		updateConstrains()
	}
	
	func updateConstrains() {
		
		var topMinus: CGFloat = 0
		if MainScreen.bounds.width > MainScreen.bounds.height {
			topMinus = 32
		} else {
			topMinus = 64
		}
		
		marginH = (self.view.frame.width - width * CGFloat(countPerH)) / CGFloat(countPerH + 1)
		marginV = (self.view.frame.height - topMinus - height * CGFloat(countPerV)) / CGFloat(countPerV + 1)
		
		var index = 0
		for v in self.view.subviews {
			
			let indexH = index % countPerH
			let indexV = index / countPerV
			let floatI = CGFloat(indexH)
			let floatJ = CGFloat(indexV)
			
			let x = CGFloat(indexH + 1) * self.marginH + floatI * self.width
			let y = CGFloat(indexV + 1) * self.marginV + floatJ * self.height + topMinus
			
//			v.snp.remakeConstraints({ [unowned self] (make) in
//				make.left.equalTo(self.view.snp.left).offset(CGFloat(indexH + 1) * self.marginH + floatI * self.width)
//				make.top.equalTo(self.view.snp.top).offset(CGFloat(indexV + 1) * self.marginV + floatJ * self.height + topMinus)
//				make.size.equalTo(CGSize(width: self.width, height: self.height))
//			})
			
			v.frame = CGRect(x: x, y: y, width: width, height: height)
			
			index = index + 1
		}
	}
	
	// MARK: - 点击事件
	
	func hitMe(_ btn: UIButton) {
		
		self.perform(#selector(removeBtn(_:)), with: btn, afterDelay: 0.25)
		
		let animGroup = CAAnimationGroup()

		let scaleAnim = CAKeyframeAnimation(keyPath: "transform.scale")
		scaleAnim.values = [1.0, 1.3, 0.1]

		let alphaAnim = CAKeyframeAnimation(keyPath: "opacity")
		alphaAnim.values = [1.0, 0.5, 1.0]

		animGroup.animations = [scaleAnim]
		animGroup.duration = 0.25
		animGroup.repeatCount = 1

		// 动画结束后，不回复状态，需要下面2句
		animGroup.isRemovedOnCompletion = false
		animGroup.fillMode = kCAFillModeBoth
		
		btn.layer.add(animGroup, forKey: "removeSelf")
	}
	
	@objc private func removeBtn(_ btn: UIButton) {
		UIView.animate(withDuration: 0.25, animations: { [unowned self] in
			btn.removeFromSuperview()
			self.view.layoutIfNeeded()

		}, completion: { [unowned self] (finished) in
			if self.view.subviews.count == 0 {
				JDToast.show("Game Over", 1.0)
			}
		})
	}
	
	// MARK: - 创建UI
	
	func createBtn() -> UIButton {
		let btn = UIButton(type: .custom)
		btn.frame.size = CGSize(width: width, height: height)
		btn.backgroundColor = UIColor.random
		btn.layer.cornerRadius = height * 0.5
		btn.layer.borderWidth = CGFloat(height > width ? height : width) * 0.25
		btn.layer.borderColor = UIColor.random.cgColor
		btn.addTarget(self, action: #selector(hitMe(_:)), for: UIControlEvents.touchUpInside)
		return btn
	}
	
}
